package com.kzc.utils;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.UUID;

import javax.management.RuntimeErrorException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

public class WebUtils {
																//RegisterForm.class
	public static <T> T request2Bean(HttpServletRequest request,Class<T> beanClass){
		
		
		try {
			
			//1.创建要封装数据的bean
			T bean = beanClass.newInstance();
			
			
			//2.把request中的数据整到bean中
			Enumeration e = request.getParameterNames();
			while (e.hasMoreElements()) {
				String name = (String) e.nextElement(); //username password email
				String value = request.getParameter(name);//aaaa  123 aa@153.com
				BeanUtils.setProperty(bean, name, value);
			}
			
			return bean;
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}
	
	public static void copyBean(Object src,Object dest){
		//注册日期转换器
		ConvertUtils.register(new Converter(){
			public Object convert(Class type, Object value) {
				if (value == null) {
					return null;
				}
				String str = (String)value;
				if (str.trim().equals("")) {
					return null;
				}
				//1999-01-01
				SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
				try {
					return df.parse(str);
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
		}, Date.class);
		
		try {
			BeanUtils.copyProperties(dest, src);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	//产生全世界唯一ID
	public static String generateID(){
		
		return UUID.randomUUID().toString();
	}

}
